import { Singleton } from "../core/Singleton";
type int = number;
//class CacheManager

    export  class CacheManager extends Singleton
    {
        constructor()
        {
            
            super();this._versionData = {};
            this._memoryData = {};
            this._saveList = [];
            return;
        }

        public readObject(name: string, version: string, arg3: string = null): Object
        {
            var loc1 = null;
            if (this._isCache)
            {
                if ((loc1 = this.getVersion(name)) == version)
                {
                    return this.readFile(name, arg3);
                }
                return null;
            }
            return null;
        }

        public writeObject(arg1: string, arg2: string, arg3: Object, arg4: string = null): void
        {
            if (this._isCache)
            {
                this._saveList.push({"n": arg1, "v": arg2, "data": arg3, "path": arg4});
                this.startTimer();
            }
            return;
        }

        /* internal  */startTimer(): void
        {
            if (this._saveTimer.running == false)
            {
                this._saveTimer.start();
            }
            return;
        }

        /* internal  */writeMemory(name: string, value: Object): void
        {
            this._memoryData[name] = value;
            return;
        }

        /* internal  */readMemory(name: string): Object
        {
            return this._memoryData[name];
        }

        /* internal  */deleteMemory(name: string): void
        {
            delete this._memoryData[name];
            return;
        }

        /* internal  */readFile(name: string, arg2: string = null): Object
        {
            var loc1 = null;
            var loc2 = null;
            if (arg2 == null)
            {
                arg2 = name;
            }
            try
            {
                loc1 = this.getLocal(arg2);
                return loc2 = loc1.data[name];
            }
            catch (e)
            {
            }
            ;
            return null;
        }

        /* internal  */writeFile(name: string, arg2: Object, arg3: string = null): Boolean
        {
            var loc1 = null;
            if (arg2 == null)
            {
                return false;
            }
            if (arg3 == null)
            {
                arg3 = name;
            }
            try
            {
                (loc1 = this.getLocal(arg3)).data[name] = arg2;
                return true;
            }
            catch (e)
            {
            }
            ;
            return false;
        }

        public clear(): void
        {
            this._saveList.length = 0;
            this._memoryData = {};
            this._versionData = {};
            this._sharedObject.data[this.CacheName] = this._versionData;
            this._sharedObject.flush();
            this._saveTimer.stop();
            return;
        }

        /* internal  */onNetStatusEvent(arg1: Object /* flash.events.NetStatusEvent */): void
        {
            if (arg1.info.code != "SharedObject.Flush.Failed")
            {
                this._sharedObject.removeEventListener(flash.events.NetStatusEvent.NET_STATUS, this.onNetStatusEvent);
                this._isCache = true;
            }
            else
            {
                this._isCache = false;
                this.clear();
            }
            this._sharedObject.data["isCache"] = this._isCache;
            return;
        }

        public flush(): void
        {
            if (this._isCache && this._writeNum > 0)
            {
                this._writeNum = 0;
                if (this._sharedObject)
                {
                    this._sharedObject.flush();
                }
            }
            return;
        }

        public static get instance(): CacheManager
        {
            if (CacheManager._instance == null)
            {
                CacheManager._instance = new CacheManager();
            }
            return CacheManager._instance;
        }

        public CacheManager.get isCache(): Boolean
        {
            return this._isCache;
        }

        public setLocalCache(): void
        {
            this.initCache();
            this.setCache(true);
            return;
        }

        /* internal  */setCache(arg1: Boolean): void
        {
            var value: Boolean;
            var state: string;

            var loc1;
            state = null;
            value = arg1;
            this.getSharedObject();
            if (this._sharedObject)
            {
                try
                {
                    this._sharedObject.addEventListener(flash.events.NetStatusEvent.NET_STATUS, this.onNetStatusEvent);
                    state = this._sharedObject.flush(this.NeedSpace);
                    if (state == flash.net.SharedObjectFlushStatus.PENDING)
                    {
                        this._sharedObject.data["isCache"] = false;
                        this._isCache = false;
                    }
                    else
                    {
                        this._sharedObject.data["isCache"] = true;
                        this._isCache = true;
                    }
                }
                catch (e)
                {
                    _isCache = false;
                    flash.system.Security.showSettings(flash.system.SecurityPanel.LOCAL_STORAGE);
                }
            }
            return;
        }

        /* internal  */getSharedObject(): Object /* flash.net.SharedObject */
        {
            var loc1;
            if (this._sharedObject == null)
            {
                try
                {
                    this._sharedObject = this.getLocal(this.CachePath);
                    this._versionData = this._sharedObject.data[this.CacheName];
                    this._isCache = this._sharedObject.data["isCache"];
                }
                catch (e)
                {
                    _isCache = false;
                }
                if (this._versionData == null)
                {
                    this._versionData = {};
                }
            }
            return this._sharedObject;
        }

        public initCache(): void
        {
            this.getSharedObject();
            if (this._saveTimer == null)
            {
                this._saveTimer = new Object /* flash.utils.Timer */(this.Delay);
                this._saveTimer.addEventListener(flash.events.TimerEvent.TIMER, this.onTimerHandler);
            }
            return;
        }

        public getLocal(name: string, arg2: string = null, secure: Boolean = false): Object /* flash.net.SharedObject */
        {
            return flash.net.SharedObject.getLocal(name, "/", secure);
        }

        /* internal  */onTimerHandler(arg1: Object /* flash.events.TimerEvent */): void
        {
            var loc1 = null;
            if (this._saveList.length > 0)
            {
                loc1 = this._saveList.shift();
                if (this.writeFile(loc1.n, loc1.data, loc1.path) != false)
                {
                    this.setVersion(loc1.n, loc1.v);
                    var loc2;
                    var loc3 = ((loc2 = this)._writeNum + 1);
                    loc2._writeNum = loc3;
                }
                else
                {
                    this.deleteVersion(loc1.n);
                }
            }
            else if (this._saveTimer.running)
            {
                this._saveTimer.stop();
            }
            return;
        }

        public setVersion(arg1: string, arg2: string): void
        {
            this._versionData[arg1] = arg2;
            if (this._sharedObject)
            {
                this._sharedObject.data[this.CacheName] = this._versionData;
            }
            return;
        }

        /* internal  */getVersion(arg1: string): string
        {
            return this._versionData[arg1];
        }

        public deleteVersion(arg1: string): void
        {
            delete this._versionData[arg1];
            if (this._sharedObject)
            {
                this._sharedObject.data[this.CacheName] = this._versionData;
            }
            return;
        }

        private /* const */NeedSpace: int = 31457300;

        private /* const */CacheName: string = "frxz2Cache";

        private /* const */CachePath: string = "/frxz2/frxz2Cache";

        private /* const */Delay: int = 500;

        private /* var */_sharedObject: Object /* flash.net.SharedObject */;

        private /* var */_versionData: Object;

        private /* var */_memoryData: Object;

        private /* var */_isCache: Boolean = false;

        private /* var */_saveTimer: Object /* flash.utils.Timer */;

        private /* var */_saveList: Array<any>;

        private /* var */localObject: Object;

        private /* var */_writeNum: int = 0;

        private static /* var */_instance: CacheManager;
    }
